<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="代理地区id" prop="aId">
      <el-input v-model="dataForm.aId" placeholder="代理地区id"></el-input>
    </el-form-item>
    <el-form-item label="微信用户id" prop="uId">
      <el-input v-model="dataForm.uId" placeholder="微信用户id"></el-input>
    </el-form-item>
    <el-form-item label="订单id" prop="hId">
      <el-input v-model="dataForm.hId" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="平台获得收益" prop="pGet">
      <el-input v-model="dataForm.pGet" placeholder="平台获得收益"></el-input>
    </el-form-item>
    <el-form-item label="用户获得收益" prop="uGet">
      <el-input v-model="dataForm.uGet" placeholder="用户获得收益"></el-input>
    </el-form-item>
    <el-form-item label="代理获得收益" prop="aGet">
      <el-input v-model="dataForm.aGet" placeholder="代理获得收益"></el-input>
    </el-form-item>
    <el-form-item label="创建日期" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建日期"></el-input>
    </el-form-item>
    <el-form-item label="收益率" prop="rate">
      <el-input v-model="dataForm.rate" placeholder="收益率"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          aId: '',
          uId: '',
          hId: '',
          pGet: '',
          uGet: '',
          aGet: '',
          createTime: '',
          rate: ''
        },
        dataRule: {
          aId: [
            { required: true, message: '代理地区id不能为空', trigger: 'blur' }
          ],
          uId: [
            { required: true, message: '微信用户id不能为空', trigger: 'blur' }
          ],
          hId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          pGet: [
            { required: true, message: '平台获得收益不能为空', trigger: 'blur' }
          ],
          uGet: [
            { required: true, message: '用户获得收益不能为空', trigger: 'blur' }
          ],
          aGet: [
            { required: true, message: '代理获得收益不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建日期不能为空', trigger: 'blur' }
          ],
          rate: [
            { required: true, message: '收益率不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/capitaltrend/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.aId = data.capitalTrend.aId
                this.dataForm.uId = data.capitalTrend.uId
                this.dataForm.hId = data.capitalTrend.hId
                this.dataForm.pGet = data.capitalTrend.pGet
                this.dataForm.uGet = data.capitalTrend.uGet
                this.dataForm.aGet = data.capitalTrend.aGet
                this.dataForm.createTime = data.capitalTrend.createTime
                this.dataForm.rate = data.capitalTrend.rate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/capitaltrend/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'aId': this.dataForm.aId,
                'uId': this.dataForm.uId,
                'hId': this.dataForm.hId,
                'pGet': this.dataForm.pGet,
                'uGet': this.dataForm.uGet,
                'aGet': this.dataForm.aGet,
                'createTime': this.dataForm.createTime,
                'rate': this.dataForm.rate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
